package com.zzxy.ssm.controller;

import com.zzxy.ssm.Service.IItemService;
import com.zzxy.ssm.Service.impl.ItemServiceImpl;
import com.zzxy.ssm.entity.Item;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/item")
public class ItemsController {

    @Autowired
    private ItemServiceImpl itemService;


    @RequestMapping("list")
    public String list(Model model){
        List<Item> itemList = itemService.findAll();
        System.out.println(itemList);
        model.addAttribute("list",itemList);
        return "/user/list";
    }

    @RequestMapping("delete")
    public String delete(Model model,Integer id){
        itemService.deleteById(id);
        List<Item> itemList = itemService.findAll();
        System.out.println(itemList);
        model.addAttribute("list",itemList);
        return "/user/list";
    }

    @RequestMapping("edit")
    public String edit(Model model,Integer id){
        Item item = itemService.findById(id);
        model.addAttribute("item",item);
        return "/user/edit";
    }

    @RequestMapping("save")
    public String save(){
        Item item = new Item();
        item.setItname("iphone");
        item.setPrice(8000.5);
        item.setDetail("好用");
        item.setCreatetime(new Date());
        itemService.saveOrUpdate(item);
        return "redirect:list.do";
    }

    @RequestMapping("update")
    public String update(Item item,Model model){
        item.setCreatetime(new Date());
        itemService.saveOrUpdate(item);
        List<Item> itemlist = itemService.findAll();
        model.addAttribute("list",itemlist);
        return "redirect:list.do";
    }
}
